package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.StuFee;
import net.skyedu.bean.Sum;
import net.skyedu.dao.AjaxDAO;

public class Pagination extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
              doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  response.setCharacterEncoding("utf-8");
		  	
		  	int currentpage = Integer.parseInt(request.getParameter("currentPage"));
	  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));
	  		
	  		int terId = Integer.parseInt(request.getParameter("terId"));
	  		int schId = Integer.parseInt(request.getParameter("schId"));
	  		int subId = Integer.parseInt(request.getParameter("subId"));
	  		int graId = Integer.parseInt(request.getParameter("graId"));
	  		
	  		String teaName = request.getParameter("teaName");
	  		String stuName = request.getParameter("stuName");
	  		String startTime = request.getParameter("startTime");
	  		String endTime = request.getParameter("endTime");
	  		
	  		StringBuffer sb = new StringBuffer();
	  		StringBuffer sb1 = new StringBuffer();
	  		if(startTime != null && !"".equals(startTime)){
	  			startTime = sb.append(startTime).append(" 00:00:00").toString();
	  		}
	  		if(endTime != null && !"".equals(endTime)){
	  			endTime = sb1.append(endTime).append(" 23:59:59").toString();
	  		}
			if(teaName != null && !"".equals(teaName)){
				teaName = URLDecoder.decode(teaName,"UTF-8");
			}
			if(stuName != null && !"".equals(stuName)){
				stuName = URLDecoder.decode(stuName,"UTF-8");
			}
         
	  		int startIndex = 0;
	  		try{
        	  startIndex = (currentpage - 1) * pagesize;
        	  
        	  AjaxDAO ad = new AjaxDAO();
//		      StringBuffer jsonAttr = new StringBuffer("{\"pageCount\":\""+pageCount+"\",\"pageData\":[");
		      StringBuffer jsonAttr = new StringBuffer("[");
		      int pageCount = 0;
		      //学生查询
        		  List<StuFee> stulist = ad.showlist(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 1);
        		  pageCount = ad.showlist(terId, schId, subId, graId, teaName, stuName, startTime, endTime, startIndex, pagesize, 0).size();
        		  if(pageCount != 0){
        			  Sum sum = ad.getSum(terId, schId, subId, graId, teaName, stuName, startTime, endTime);
    			      for(int i = 0; i < stulist.size(); i++){
    			    	  StuFee stu = stulist.get(i);
    			    	  jsonAttr.append("{");
            			  jsonAttr.append("\"should\":\""+sum.getSumShould()+"\",");
            			  jsonAttr.append("\"actual\":\""+sum.getSumActual()+"\",");
            			  jsonAttr.append("\"cln\":\""+sum.getSumClaNum()+"\",");
            			  int sumYiNum = ad.count(terId, 0, 0);
            			  int synum = sum.getSumClaNum() - sumYiNum;
            			  
            			  jsonAttr.append("\"sumYiNum\":\""+sumYiNum+"\",");
            			  jsonAttr.append("\"synum\":\""+synum+"\",");
            			  
    			    	  jsonAttr.append("\"stuId\":\""+stu.getStuId()+"\",");
    			    	  jsonAttr.append("\"stuName\":\""+stu.getStuName()+"\",");
    			    	  jsonAttr.append("\"terName\":\""+stu.getTerName()+"\",");
    			    	  jsonAttr.append("\"schName\":\""+stu.getSchName()+"\",");
    			    	  jsonAttr.append("\"subName\":\""+stu.getSubName()+"\",");
    			    	  jsonAttr.append("\"graName\":\""+stu.getGraName()+"\",");
    			    	  jsonAttr.append("\"claName\":\""+stu.getClaName()+"\",");
    			    	  jsonAttr.append("\"teaName\":\""+stu.getTeaName()+"\",");
    			    	  jsonAttr.append("\"shouldFee\":\""+stu.getShouldFee()+"\",");
    			    	  jsonAttr.append("\"actualFee\":\""+stu.getActualFee()+"\",");
    			    	  jsonAttr.append("\"discount\":\""+stu.getDiscount()+"\",");
    			    	  String payType = "";
    			    	  if(stu.getPayType() == 1){payType = "现金";}
    			    	  if(stu.getPayType() == 2){payType = "转账";}
    			    	  if(stu.getPayType() == 3){payType = "支票";}
    			    	  jsonAttr.append("\"payType\":\""+payType+"\",");
    			    	  jsonAttr.append("\"payTime\":\""+stu.getPayTime()+"\",");
    			    	  jsonAttr.append("\"claNum\":\""+stu.getClaNum()+"\",");
//    			    	  System.out.println(stu.getStuId()+"----"+stu.getClaId());
    			    	  int num = ad.count(terId,stu.getStuId() ,stu.getClaId());
    			    	  if(num != 0){
    			    		  jsonAttr.append("\"yiNum\":\"<a href='javascript:void(0);' " +
    			    		  "onclick='openWin("+terId+","+stu.getStuId()+","+stu.getClaId()+");'><font color='red' style='font-weight:bold;'>"+num+"</font></a>\",");
    			    	  }else{
    			    		  jsonAttr.append("\"yiNum\":\"0\",");
    			    	  }
    			    	  int syNum = stu.getClaNum() - num;
    			    	  jsonAttr.append("\"syNum\":\""+syNum+"\",");
    			    	  jsonAttr.append("\"recordStaff\":\""+stu.getReceiver()+"\",");
    			    	  
    			    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
    			    	  jsonAttr.append("}");
    			    	  if(i<stulist.size()-1){
    			    		  jsonAttr.append(",");
    			    	  }
    			       }
        	  }else{
		    	  jsonAttr.append("{");
		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
		    	  jsonAttr.append("}");
		      }
		      
		      jsonAttr.append("]");
//		      System.out.println("JSON:---"+jsonAttr);
		      PrintWriter out = response.getWriter();
		      out.print(jsonAttr);
		      out.flush();
		      out.close();
        	
          }catch(Exception e){
        	  //写到日志表中
        	  e.printStackTrace();
          }
       
		
		
		
		
	
	}
	
	
	
	
	

	public void init() throws ServletException {}
}
